package net.b521.petshop.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import net.b521.petshop.dao.ClassificationMapper;
import net.b521.petshop.dao.ProductMapper;
import net.b521.petshop.dao.model.*;
import net.b521.petshop.service.ProductService;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("productService")
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ClassificationMapper classificationMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;


    @Override
    public Product findById(int id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Product> findAll() {
        // return productDao.findAll(pageable);
        List<Product> products = productMapper.selectByExample(null);
        String sql = sqlSessionFactory.getConfiguration()
                .getMappedStatement("net.b521.petshop.dao.ProductMapper.selectByExample")
                .getBoundSql(null).getSql();
        System.out.println(sql);
        return products;
    }

    @Override
    public List<Product> findBySeller(Seller seller) {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria()
                .andSellerIdEqualTo(seller.getId());
        List<Product> products = productMapper.selectByExample(productExample);
        return products;
    }

    /**
     * 查找热门商品
     *
     * @return
     */
    @Override
    public List<Product> findHotProduct() {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andIsHotEqualTo(1);
        List<Product> products = productMapper.selectByExample(productExample);
        System.out.println(JSON.toJSONString(products));
        return products;
    }

    /**
     * 查找商品
     * @param word
     * @return
     */
    @Override
    public List<Product> findProduct(String word) {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andTitleLike("%" + word + "%");
        List<Product> products = productMapper.selectByExample(productExample);
        System.out.println(JSON.toJSONString(products));
        return products;
    }

    /**
     * 查找最新商品
     * @return
     */
    @Override
    public List<Product> findNewProduct() {
        // 查找两周内上架的商品
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DAY_OF_MONTH, -14);
        ProductExample productExample = new ProductExample();
        productExample.setOrderByClause("pdate desc");
        List<Product> products = productMapper.selectByExample(productExample);
        // return productDao.findNew(pageable);
        return products;
    }




    /**
     * 根据一级分类查找商品
     *
     * @param cid
     * @return
     */
    @Override
    public List<Product> findByCid(int cid) {
        //查找出所有二级分类
        ClassificationExample ClassificationExample = new ClassificationExample();
        ClassificationExample.createCriteria().andParentIdEqualTo(cid);
        List<Classification> sec = classificationMapper.selectByExample(ClassificationExample);
        // List<Classification> sec = classificationDao.findByParentId(cid);
        List<Integer> secIds = Lists.newArrayList();
        for (Classification Classification : sec) {
            secIds.add(Classification.getId());
        }

        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andCsidIn(secIds);
        List<Product> products = productMapper.selectByExample(productExample);

        // return productDao.findByCsidIn(secIds,pageable);
        return products;
    }

    /**
     * 根据二级分类查找商品
     *
     * @param csid
     * @return
     */
    @Override
    public List<Product> findByCsid(int csid) {

        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andCsidEqualTo(csid);
        List<Product> products = productMapper.selectByExample(productExample);
        // return productDao.findByCsid(csid,pageable);
        return products;
    }


    @Override
    public void update(Product product) {
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public int create(Product product) {
        // return productDao.save(product).getId();
        productMapper.insertSelective(product);
        return product.getId();
    }

    @Override
    public void delById(int id) {
        productMapper.deleteByPrimaryKey(id);
    }
}
